package rickyGame.net
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import rickyGame.net.handler.*;

	/**
	 * 消息处理类
	 */
	public class WorkQueue
	{
		private static var _instance:WorkQueue
		
		public static function get instance():WorkQueue
		{
			if(!_instance)
			{
				HandlerFactory.registerALL();
				_instance=new WorkQueue();
			}
			return _instance;
		}

		public function WorkQueue()
		{
			_queue=new Array();

			queueTimer=new Timer(5);
			queueTimer.addEventListener(TimerEvent.TIMER,onProccessQueue);
			queueTimer.start();
		}
		
		/**
		 * 函数放入队列
		 */
		public function pushRecive(msg:Object):void
		{
			_queue.push(msg);
		}
		
		/**
		 * 处理队列中的消息
		 */
		private function onProccessQueue(e:TimerEvent):void
		{
			if(this.queue.length>0)
			{
				var msg:Object=this.queue.shift();
				HandlerFactory.call(msg);
			}
		}
		
		
		/**
		 * 消息队列
		 */
		public function get queue():Array
		{
			return _queue;
		}
		
		/**
		 * @private
		 */
		public function set queue(value:Array):void
		{
			_queue = value;
		}
		
		//private
		private var _queue:Array=new Array();
		
		/**
		 * 队列消息定时器
		 */
		private var queueTimer:Timer;
	}
}